/*******************************************************************************
 * Copyright (c) 2008, 2009 Tapestry IDE Developers
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Tapestry IDE Developers - initial API and implementation
 *******************************************************************************/
package com.googlecode.tapestry.ide.core.model;

import org.eclipse.core.runtime.ListenerList;

import com.googlecode.tapestry.ide.core.model.ModelChangeEvent.Type;

/**
 * Default implementation of the common protocol for a model.
 */
public abstract class AbstractModel extends AbstractModelElement implements
		IModel {

	/** The listeners. */
	private ListenerList listeners;

	/**
	 * Instantiates a new abstract model.
	 * 
	 * @param parent
	 *            the parent
	 * @param name
	 *            the name
	 */
	public AbstractModel(IModelElement parent, String name) {
		super(parent, name);
		listeners = new ListenerList();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @seecom.googlecode.tapestry.ide.core.model.IModel#addChangeListener(com.
	 * googlecode.tapestry.ide.core.model.IModelChangeListener)
	 */
	public final void addChangeListener(IModelChangeListener listener) {
		listeners.add(listener);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.googlecode.tapestry.ide.core.model.IModel#removeChangeListener(com
	 * .googlecode.tapestry.ide.core.model.IModelChangeListener)
	 */
	public final void removeChangeListener(IModelChangeListener listener) {
		listeners.remove(listener);
	}

	/**
	 * Notify listeners.
	 * 
	 * @param element
	 *            the element
	 * @param type
	 *            the type
	 */
	public final void notifyListeners(IModelElement element, Type type) {
		ModelChangeEvent event = new ModelChangeEvent(element, type);
		for (Object listener : listeners.getListeners()) {
			((IModelChangeListener) listener).elementChanged(event);
		}
	}
}
